package com.yjkf.zh.admin.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yjkf.zh.admin.dao.SysDeviceMapper;
import com.yjkf.zh.admin.dao.SysProductMapper;
import com.yjkf.zh.admin.model.SysDevice;
import com.yjkf.zh.admin.model.SysProduct;
import com.yjkf.zh.admin.service.SysDeviceService;
import com.yjkf.zh.core.page.MybatisPageHelper;
import com.yjkf.zh.core.page.PageRequest;
import com.yjkf.zh.core.page.PageResult;
import com.yjkf.zh.core.utils.DeviceIdentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDeviceServiceImpl implements SysDeviceService {

    @Autowired
    private SysDeviceMapper sysDeviceMapper;

    @Autowired
    private SysProductMapper sysProductMapper;

    @Override
    public int save(SysDevice record) {
        String deviceName=record.getDeviceName();
        DeviceIdentUtil deviceIdentUtil = new DeviceIdentUtil();
        String ident=deviceIdentUtil.getDeviceString(deviceName);
        record.setIdent(ident);
        if(record.getId()==null||record.getId()==0) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4FteAmT72WrKLJkj5QdV", "O3PxeWvPTHajKwwZ4AS4JlfRSFe98C");
            SysProduct sysProduct= sysProductMapper.selectKeyForId(record.getProductId());
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.GET);
            request.setSysDomain("iot.cn-shanghai.aliyuncs.com");
            request.setSysVersion("2018-01-20");
            request.setSysAction("RegisterDevice");
            request.putQueryParameter("RegionId", "cn-shanghai");
            request.putQueryParameter("ProductKey", sysProduct.getProductKey());
            request.putQueryParameter("DeviceName", deviceName);
            //request.putQueryParameter("Nickname", "API_test01");
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return sysDeviceMapper.insertDevice(record);
        }else{
            return sysDeviceMapper.updateDevice(record);
        }
    }

    @Override
    public int delete(SysDevice record) {
        return sysDeviceMapper.deleteDevice(record.getId());
    }

    @Override
    public int delete(List<SysDevice> records) {
        for(SysDevice sysDevice:records){
            delete(sysDevice);
        }
        return 1;
    }

    @Override
    public SysDevice findById(Long id) {
        return null;
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {

        PageResult pageResult=null;
        Object name = pageRequest.getParam("deviceName");
        Map<Object, Object> params = new HashMap<>();
        params.put("id",1L);
        pageRequest.setParams(params);
        Object id = pageRequest.getParam("id");
        if(name!=null){
            pageResult = MybatisPageHelper.findPage(pageRequest,sysDeviceMapper,"findPageDevices",name,id);
        }else{
           pageResult = MybatisPageHelper.findPage(pageRequest,sysDeviceMapper,"findDevices",id);
        }

        return pageResult;
    }

   // @Cacheable(value = "device",key = "#id")
    @Override
    public List<SysDevice> findDevices(Long id) {
        return sysDeviceMapper.findDevices(id);
    }

    //@Cacheable(value = "devicePage")
    @Override
    public PageResult findPage2(PageRequest pageRequest, Long userId) {
        PageResult pageResult=null;
        Object name = pageRequest.getParam("deviceName");
        Map<Object, Object> params = new HashMap<>();
        params.put("id",userId);
        pageRequest.setParams(params);
        Object id = pageRequest.getParam("id");
        if(userId==1){
            if(name!=null){
                pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findPageAdmin", name);
            }else{
                pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findPageByAdmin");
            }
        }else {
            if (name != null) {
                pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findPageDevices", name, id);
            } else {
                pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findDevices", id);
            }
        }
        return pageResult;
    }

    @Override
    public PageResult findPageDeviceForAdmin(PageRequest pageRequest) {
        PageResult pageResult=null;
        Object name = pageRequest.getParam("deviceName");
        if(name != null){
            pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findPageDeviceForAdmin", name);
        }else{
            pageResult = MybatisPageHelper.findPage(pageRequest, sysDeviceMapper, "findPageByDeviceForAdmin");
        }
        return pageResult;
    }


}
